import axios  from "axios";
import { addRiverWater,clearAll } from './riverWater'

export default class Effect2 {
  constructor(viewer){
    this.viewer = viewer

    this.waterEffect = null
    this.linePrimitive = null
    this.linePrimitive1 = null
    
    this.start()
  }

  start(type){
    //添加河堤
    this.addhedi()
    
    clearAll()
    //增加河面 
    addRiverWater(this.viewer,1);
  }


   //增加河堤
   addhedi(){
    if(this.linePrimitive) return
    axios.get('data/river/difang4490re.json').then((data)=>{
      var geojson = new Cesium.Geojson();
      geojson = geojson.read(data.data);
      var option = option ? option : {};
      option.asynchronous = option.asynchronous ? option.asynchronous : false;
      option.clampToGround = option.clampToGround ? option.clampToGround : false;
      option.fillColor = option.fillColor ? option.fillColor : '#ff0000';
      option.fillOpacity = option.hasOwnProperty('fillOpacity') ? option.fillOpacity : 1.0;
      option.pixelSize = option.pixelSize ? option.pixelSize : 10;
      option.strokeColor = option.strokeColor ? option.strokeColor : '#ff0000';
      option.strokeOpacity = option.hasOwnProperty('strokeOpacity') ? option.strokeOpacity : 1.0;
      option.lineWidth = option.hasOwnProperty('lineWidth') ? option.lineWidth : 2.0;
      option.outlineWidth = option.hasOwnProperty('outlineWidth') ? option.outlineWidth : 0;
      option.translucent = false;
      if (option.fillOpacity != 1.0 || option.strokeOpacity != 1.0) {
          option.translucent = true;
      }

      let geometryInstances = [];
      for (let i = 0; i < geojson.lineArray.length; i++) {
          let geometry = geojson.lineArray[i].geometry;

          var corridor = new Cesium.CorridorGeometry({
              cornerType:1,
              positions : Cesium.Cartesian3.fromDegreesArrayHeights(geometry),
              topWidth:8,
              width: 3,
              height: 5,
              extrudedHeight: 42
          });

          let geometryInstance = new Cesium.GeometryInstance({
              geometry: corridor,
              attributes: {
                  color: Cesium.ColorGeometryInstanceAttribute.fromColor(
                      Cesium.Color.fromCssColorString('#bbbbbb')
                  ),
              },
          });

          geometryInstances.push(geometryInstance);
      }


      this.linePrimitive = new Cesium.Primitive({
          geometryInstances: geometryInstances,
          undisplayable: true,
          appearance: new Cesium.PerInstanceColorAppearance({
              flat: false,
              translucent: false,
              closed: true
          })
      });

      this.viewer.scene.primitives.add(this.linePrimitive);


      var corridor1 = new Cesium.CorridorGeometry({
          cornerType:1,
          positions : Cesium.Cartesian3.fromDegreesArray([
              113.122600779, 27.7389653870001,
              113.122265987,27.7395677870001
          ]),
          topWidth:10,
          width: 30,
          height: 30,
          extrudedHeight: 65
      });

      let geometryInstance1 = new Cesium.GeometryInstance({
          geometry: corridor1,
          attributes: {
              color: Cesium.ColorGeometryInstanceAttribute.fromColor(
                  Cesium.Color.fromCssColorString('#bbbbbb').withAlpha(0.01)
              ),
          },
      });

      this.linePrimitive1 = new Cesium.Primitive({
          geometryInstances: [geometryInstance1],
          undisplayable: true,
          appearance: new Cesium.PerInstanceColorAppearance({
              flat: false,
              translucent: true,
              closed: true
          })
      });

      this.viewer.scene.primitives.add(this.linePrimitive1);
    })
  }


  clear(){
    this.linePrimitive && this.viewer.scene.primitives.remove(this.linePrimitive);
    this.linePrimitive1 && this.viewer.scene.primitives.remove(this.linePrimitive1);
  }
}